﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace LiveStream.Caching
{
    class CacheAsyncResult : IAsyncResult
    {
        public string strUrl { get; set; }

        public object AsyncState { get; private set; }

        public WaitHandle AsyncWaitHandle { get { return _completeEvent; } }

        public bool CompletedSynchronously { get; private set; }

        public bool IsCompleted { get; private set; }

        // Contains the output result of the GetChunk API
        public Object Result { get; private set; }

        internal TimeSpan Timestamp { get; private set; }

        // Callback function when GetChunk is completed. Used in asynchronous mode only.
        // Null for synchronous mode.
        private AsyncCallback _callback;

        // Event is used to signal the completion of the operation
        private ManualResetEvent _completeEvent = new ManualResetEvent(false);

        // Called when the operation is completed
        public void Complete(Object result, bool completedSynchronously)
        {
            Result = result;
            CompletedSynchronously = completedSynchronously;

            IsCompleted = true;
            _completeEvent.Set();

            if (null != _callback) { ;  }
        }
    }
}
